#include <algorithm>
#include <cstdio>
#include <cstring>

typedef long long ll;
const int N = 1505, mod = 1e9 + 7;
int ans, t, n, m, p, k, invm, invm1;
ll f[N], P[N], fact[N];
ll a[N][N];

inline ll qpow(ll base, ll exp) {
  ll res = 1;
  while (exp) {
    if (exp & 1) (res *= base) %= mod;
    exp >>= 1;
    (base *= base) %= mod;
  }
  return res;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("CppTest.in", "r", stdin);
  freopen("CppTest.out", "w", stdout);
#endif
#endif

  scanf("%d", &t);
  fact[1] = 1;
  for (int i = 2; i < N; ++i) fact[i] = 1ll * fact[mod % i] * (mod - mod / i) % mod;
  while (t--) {
    memset(a, 0, sizeof(a));
    memset(P, 0, sizeof(P));
    ans = 0;
    scanf("%d%d%d%d", &n, &p, &m, &k);
    if (k == 0 || (m == 0 && k == 1)) {
      puts("-1");
      continue;
    }
    if (m == 0) {
      while (p > 0) {
        if (p < n) ++p;
        p -= k, ++ans;
      }
      printf("%d\n", ans);
      continue;
    }
    invm = qpow(m, mod - 2), invm1 = qpow(m + 1, mod - 2);
    P[0] = qpow(1ll * m * invm1 % mod, k);
    for (int i = 1; i <= std::min(n, k); ++i) {
      P[i] = 1ll * P[i - 1] * invm % mod * fact[i] % mod * (k - i + 1 + mod) % mod;
    }
    for (int i = 1; i < n; ++i) {
      for (int j = 0; j < i; ++j)
        a[i][i - j] = 1ll * (1ll * P[j] * m % mod + P[j + 1]) % mod * invm1 % mod;
      a[i][i + 1] = 1ll * P[0] * invm1 % mod;
    }
    for (int i = 1; i <= n; ++i) {
      a[n][i] = P[n - i];
      a[i][i] = (a[i][i] - 1 + mod) % mod;
      a[i][n + 1] = mod - 1;
    }
    int inv;
    for (int i = 1; i < n; i++) {
      inv = qpow(a[i][i], mod - 2);
      a[i][n + 1] = (a[i][n + 1] * inv) % mod;
      a[i][i + 1] = (a[i][i + 1] * inv) % mod;
      a[i][i] = 1;
      for (int j = i + 1; j <= n; j++) {
        a[j][n + 1] = (a[j][n + 1] + mod - a[i][n + 1] * a[j][i] % mod) % mod;
        a[j][i + 1] = (a[j][i + 1] + mod - a[i][i + 1] * a[j][i] % mod) % mod;
        a[j][i] = 0;
      }
    }
    inv = qpow(a[n][n], mod - 2);
    a[n][n + 1] = (a[n][n + 1] * inv) % mod;
    a[n][n] = 1;
    for (int i = n - 1; i >= p; i--) {
      a[i][n + 1] = (a[i][n + 1] + mod - a[i + 1][n + 1] * a[i][i + 1] % mod) % mod;
      a[i][i] = 1;
    }
    printf("%lld\n", a[p][n + 1]);
  }
  return 0;
}